<?php
/**
 * Created by PhpStorm.
 * User: papa
 * Date: 18/5/21
 * Time: 17:07
 */

namespace app\api\model;


class Order extends BaseModel
{
    protected $visible = ['id','sn','type','is_comment','status','true_price','order_detail','goods','points'];

    //关联订单商品表
    public function orderDetail()
    {
        return $this->hasMany('OrderDetail','order_id','id');
    }



    //获取器，点餐状态
    public function getTypeAttr($value)
    {
        $type = [1 => '堂食',2 => '外卖',3 => '自取'];
        return $type[$value];
    }

    //获取器，评论状态
    public function getIsCommentAttr($value)
    {
        $IsComment = [1=>'未评价',2=>'已评价'];
        return $IsComment[$value];
    }

    //获取器，订单状态
    public function getStatusAttr($value)
    {
//        $status = [1=>'待付款',2=>'已付款',3=>'配送中',4=>'订单完成',5=>'退款完成,订单关闭',6=>'退款中',7=>'订单关闭',8=>'待接单',9=>'申请退款',10=>'待领取'];
        $status = [1=>'待付款',2=>'客户已付款,待发货',3=>'配送中',4=>'订单完成',5=>'商家审核退款',6=>'审核通过,退款中',7=>'退款成功',8=>'订单关闭'];
        return $status[$value];
    }

    //对超时的订单用户没有确认收获的自动确认收获
    public static function autoConfirmOrder($uid)
    {
        $orders = self::field('id,create_time')->where(['status'=>3,'user_id'=>$uid])->select();
        foreach($orders as $k=>$v){
            $create_time = strtotime($v->getData('create_time'));
            $now = time();
            if(($now - $create_time) > (config('setting.auto_confirm_time') * 24 * 60 * 60)){
                $id = $v->getData('id');
                self::where(['id'=>$id])->update(['status'=>4]);
            }
        }
    }

    //订单的失效时间
    public static function abateOrder($arr,$time)
    {
        $orders = self::field('id,create_time')->where($arr)->select();
        $now = time();
        foreach($orders as $k=>$v){
            $create_time = strtotime($v->getData('create_time'));
            if(($now - $create_time) > $time){
                $id = $v->getData('id');
                self::where(['id'=>$id])->update(['status'=>8]);
            }
        }
    }

}